<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Some functions are derived from CakePHP,
 * Copyright (c) 2006, Cake Software Foundation, Inc.
 * @link http://cakephp.org/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Base Helper class.
 */
abstract class Helper
{
	protected $_tags = array();
	protected $controllers;
	
	public $name;
	public $webroot = '/';
	public $here;
	public $data;
	public $params;
	
	function __construct(View $view=null)
	{
		$this->name = str_replace('Helper', '', get_class($this));
		
		if (!is_null($view))
		{
			$this->here   =  $view->here;
			$this->params =& $view->params;
			
			# validation errors
			if (isset($view->controller))
			{
				$this->controller =  $view->controller;
				$this->data       =& $view->controller->data;
			}
			
			# binds itself to view actions
			if (!is_null($view) and !empty($this->binds))
			{
				if (!is_array($this->binds))
					$this->binds = explode(',', $this->binds);
				foreach ($this->binds as $bind)
					$view->bindAction($this->name, trim($bind));
			}
		}
	}
	
	// tags
	
	function __call($tag, $args)
	{
		$type = in_array($tag, $this->_tags) ? 'tag' : 'contentTag';
		return call_user_func_array(array($this, $type), $args);
	}
	
	function tag($tag, $attr=null)
	{
		return "<$tag".$this->_parseAttributes(&$attr)." />";
	}
	
	function contentTag($tag, $content, $attr=null)
	{
		return "<$tag".$this->_parseAttributes(&$attr).">$content</$tag>";
	}
	
	protected function _parseAttributes($attr=null)
	{
		if (!empty($attr))
		{
			$str = '';
			foreach($attr as $p => $v)
				$str.= " $p=\"$v\"";
			return $str;
		}
	}
	
	// links
	
	function url($url/*, $link=false*/)
	{
		# do not process URL
		if ($url{0} == '#' or strpos($url, '://') or strpos($url, 'javascript:') === 0)
			return $url;
		
		# prefix controller's path
		if ($url{0} != '/')
			$url = "/{$this->controller->viewPath}/$url";
		
		# prefix plugin's path
#		if ($link and isset(Load::$plugin))
#			$url = "/{Load::$plugin}$url";
		
		# fixes amps.
		return preg_replace('/&([^a])/', '&amp;\1', $url);
	}
	
	/**
	 * Formats an HTML link.
	 * 
	 * @return string
	 * @param $title string
	 * @param $url string[optional] 
	 * @param $attr array[optional] Additional attributes.
	 * @param $confirm string[optional] Text for an optional javascript confirmation (useful for deletions).
	 */
	function link($title, $url=null, array $attr=null, $confirm=null)
	{
		if (is_null($url))
			$url = $title;
		$attr['href'] = $this->url($url/*, true*/);
		
		# javascript confirm
		if (isset($confirm))
		{
			$confirm = htmlspecialchars($confirm, ENT_NOQUOTES);
			$confirm = str_replace(array("'", '"'), array("\'", '&quot;'), $confirm);
			$attr['onclick'] = "return confirm('$confirm');";
		}
		
		return $this->contentTag('a', $title, &$attr);
	}
}
?>